﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using PaymentHelper.Core.Extension;
using PaymentHelper.Core.ExceptionHandler;

namespace PaymentHelper.PaymentFlow
{
    public static class PaymentFlowManager
    {
        private static IOrderHandler _orderHandler;
        private static string _layout;

        public static IOrderHandler OrderHandler
        {
            get { return PaymentFlowManager._orderHandler; }
            set { PaymentFlowManager._orderHandler = value; }
        }

        public static string Layout 
        {
            get 
            {
                return _layout;
            }
            set 
            {
                _layout = value;
            }
        }

        public static IList<BasePaymentFlow> LoadAllPaymentFlowTemplates(bool onlyInstalled=true, bool onlyEnable=true)
        {
            try
            {
               return  ExtensionManager.LoadAllExtensions<BasePaymentFlow>(onlyInstalled, onlyEnable);
            }
            catch (PaymentHelperException ex)
            {
                throw ex;
            }
        }

        public static BasePaymentFlow LoadDefaultPaymentFlow()
        {
            string defaultSystemName = ExtensionManager.LoadSystemNameDefaultPaymentFlow();
            return GetPaymentFlowTemplateByName(defaultSystemName);
        }

        public static BasePaymentFlow GetPaymentFlowTemplateByName(string systemName, bool onlyInstalled = true, bool onlyEnable = true)
        {
            try
            {
                return ExtensionManager.LoadExtensionBySystemName<BasePaymentFlow>(systemName, onlyInstalled, onlyEnable);
            }
            catch (PaymentHelperException ex)
            {
                throw ex;
            }
        }

        public static void RegisterRoutes(RouteCollection routeCollection)
        {
            foreach (var extension in LoadAllPaymentFlowTemplates())
            {
                extension.RegisterRoute(routeCollection);
            }
        }
    }
}
